<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <h2>五一倒计时</h2>
      <p></p>
    </div>
    <script>
      function diffTime(curr, tar) {
        //curr表示当前时间
        //tar表示目标时间
        var diffMs = Math.ceil((tar.getTime() - curr.getTime()) / 1000);
        //console.log(diffMs); //秒
        //推算出hour minute second
        var day = Math.floor(diffMs / (24 * 60 * 60)); //1.5   1天 xx小时
        var hours = Math.floor((diffMs % (24 * 60 * 60)) / (60 * 60));
        var minutes = Math.floor((diffMs % (60 * 60)) / 60);
        var seconds = diffMs % 60;
        return `还剩${day}天${hours}时${minutes}分${seconds}秒`;
      }
      var time2 = new Date(2023, 4, 1, 00, 00, 00);
      setInterval(function () {
        var p = document.querySelector('p');
        var time1 = new Date();
        var diff = diffTime(time1, time2);
        p.innerHTML = diff;
      }, 1000);
    </script>
  </body>
</html>
